package zs.zsxmlv.service;

import common.zsxmlv.utils.PropertiesUtil;
import common.zsxmlv.utils.Utils;
import common.zsxmlv.vo.Table;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import common.zsxmlv.enums.ResultEnum;
import zs.zsxmlv.mapper.NoteMapper;
import zs.zsxmlv.po.Note;
import common.zsxmlv.vo.Result;
import zs.zsxmlv.util.FileUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: ZSXM
 * @Description:
 * @Company: DingZheng
 * @Date: Create by 2018/5/22 0:14
 * @Version: 1.0
 * @Modify by:
 */
@Service
@Transactional
public class NoteService {

    @Resource
    private NoteMapper noteMapper;

    /**
     * 保存笔记
     * @param content 笔记内容
     * @param note 笔记模型类
     * @return 返回 result 对象
     */
    public Result insertNote(String content, Note note){
        boolean flag = false;
        String msg = null;
        if (content != null && note != null){
            String fileName = Utils.generateFileName();
            FileUtils.writeFile(content, fileName, PropertiesUtil.getValue("config.properties","notePath"));
            Note n = new Note();
            n.setId(Utils.generateUUID());
            n.setTitle(note.getTitle());
            n.setPid(note.getPid());
            n.setSort(note.getSort());
            n.setDocUrl(fileName);
            int insert = noteMapper.insert(n);
            flag = insert == 1 ? true : false;
            msg = flag ? "保存成功" : "保存失败";
        }else{
            msg = ResultEnum.PARAM_ERROR.getMsg();
        }
        return new Result(flag,msg);
    }


    /**
     * 分页查询
     * @param page 页码
     * @param limit 每页大小
     * @return layui data table
     */
    public Table selectByList(Integer page, Integer limit) {
        Table table = new Table();
        boolean condition = page != null && page != 0 && limit != null && limit != 0;
        if (condition){
            HashMap<Object, Object> map = new HashMap<>();
            map.put("startRecord", (page - 1) * limit);
            map.put("pageSize", limit);
            List<Note> notes = noteMapper.selectByPage(map);
            if (notes.isEmpty()){
                table.setCode(ResultEnum.NOT_FOUND.getCode());
                table.setMsg(ResultEnum.NOT_FOUND.getMsg());
            }else{
                table.setCount(notes.size());
                table.setData(notes);
                table.setCode(ResultEnum.SUCCESS.getCode());
                table.setMsg(ResultEnum.SUCCESS.getMsg());
            }
        }else {
            table.setMsg(ResultEnum.PARAM_ERROR.getMsg());
            table.setCode(ResultEnum.PARAM_ERROR.getCode());
        }
        return table;
    }

    /**
     * 根据笔记ID查询note
     * @param id 笔记ID(32位随机字符串UUID)
     * @return 笔记详细信息
     */
    public Result selectByPrimaryKey(String id) {
        Result result = new Result();
        if (id != null){
            Note note = noteMapper.selectByPrimaryKey(id);
            if (note != null){
                String content = FileUtils.readFile(PropertiesUtil.getValue("config.properties", "notePath") + note.getDocUrl());
                note.setDocUrl(content);
                result.setSuccess(true);
                result.setData(note);
                result.setMsg(ResultEnum.SUCCESS.getMsg());
            }else {
                result.setSuccess(false);
                result.setMsg(ResultEnum.NOT_FOUND.getMsg());
            }
        }else{
            result.setSuccess(false);
            result.setMsg(ResultEnum.PARAM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 根据id删除数据
     * @param id 笔记id
     * @return result
     */
    public Result deleteById(String id) {
        Result result = new Result();
        if (id != null) {
            Note note = noteMapper.selectByPrimaryKey(id);
            if (note != null) {
                boolean flag = noteMapper.deleteByPrimaryKey(id) == 1 ? true : false;
                if (flag) {
                    boolean deleteFile = FileUtils.deleteFile(PropertiesUtil.getValue("config.properties", "notePath") + note.getDocUrl());
                    if (deleteFile) {
                        System.out.println("笔记文件删除成功" + note.getDocUrl());
                    } else {
                        System.out.println("笔记文件删除失败" + note.getDocUrl());
                    }
                    result.setSuccess(true);
                    result.setMsg(ResultEnum.DELETE_SUCCESS.getMsg());
                }else {
                    result.setSuccess(false);
                    result.setMsg(ResultEnum.DELETE_ERROR.getMsg());
                }
            }else {
                result.setSuccess(false);
                result.setMsg(ResultEnum.NOT_FOUND_DELETE_ERROR.getMsg());
            }
        }else {
            result.setSuccess(false);
            result.setMsg(ResultEnum.PARAM_ERROR.getMsg());
        }
        return result;
    }

}
